package com.yanqu.road.server.cmd.manor.fishpond;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.manor.FishPondProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.manor.ManorFishPondModel;
import com.yanqu.road.server.gameplayer.module.manor.ManorModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @author ：zfr
 * @date ：2021/10/12 14:15
 */
@Cmd(code = GameProtocol.S_FISH_POND_FISHING, desc = "鱼塘钓鱼")
public class FishingCmd extends AbstractCommand {
    private int times;
    private int itemId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        FishPondProto.FishingReqMsg reqMsg = FishPondProto.FishingReqMsg.parseFrom(packet.getBytes());
        times = reqMsg.getTimes();
        itemId = reqMsg.getItemId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ManorFishPondModel manorFishPondModel = player.getModule(ManorModule.class).getManorFishPondModel();
        if (manorFishPondModel != null && manorFishPondModel.getFishPondData() != null && manorFishPondModel.getFishPondData().getFishPondLv() != 0) {
            if(itemId == 0){
                itemId = GameConfig.FISHPOND_FISHING_ITEM_ID;
            }
            ret = manorFishPondModel.fishing(times,itemId);
        } else {
            ret = GameErrorCode.E_FISH_POND_IS_LOCK;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            FishPondProto.FishingRespMsg.Builder respMsg = FishPondProto.FishingRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_FISH_POND_FISHING, respMsg);
        }
    }
}
